package com.gugee.tiktok.web.service;


import com.gugee.tiktok.web.dto.*;
import com.gugee.tiktok.web.repository.*;
import com.gugee.tiktok.web.model.mysql.AwemeCollection;
import com.gugee.tiktok.web.model.mysql.BloggerCollection;
import com.gugee.tiktok.web.model.mysql.ChallengeCollection;
import com.gugee.tiktok.web.model.mysql.MusicCollection;
import com.xiguaji.boot.toolkit.jdbc.Page;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CollectionService {

    @Autowired
    private AwemeCollectionRepository awemeCollectionRepository;

    @Autowired
    private BloggerCollectionRepository bloggerCollectionRepository;

    @Autowired
    private MusicCollectionRepository musicCollectionRepository;

    @Autowired
    private BloggerRepository bloggerRepository;

    @Autowired
    private ChallengeCollectionRepository challengeCollectionRepository;


    //####################################################AwemeCollect START####################################################
    public Page<AwemeCollectResponse> getAwemePage(CollectSearchData searchData, Long userId){
        val page = awemeCollectionRepository.getPage(searchData, userId);
        page.getList().forEach( a ->{
            val blogger = bloggerRepository.findByOUid(a.getUid());
            a.setUniqueId(blogger.get().getOUniqueId());
        });
        return page;
    }

    public long addAwemeCollect(AwemeCollection awemeCollection){
        return awemeCollectionRepository.save(awemeCollection).getId();
    }

    public AwemeCollection getAwemeCollection(long awemeId,long userId){
        val awemeCollectionOptional = awemeCollectionRepository.findByAwemeIdAndUserId(awemeId, userId);
        return awemeCollectionOptional.isPresent()?awemeCollectionOptional.get():null;
    }

    public void cancelCollectAweme(Long id){
        awemeCollectionRepository.deleteById(id);
    }
    public int getAwemeCollectCount(long userId){
        return awemeCollectionRepository.countByUserId(userId);
    }
    //####################################################AwemeCollect END####################################################


    //####################################################BloggerCollect START####################################################
    public long addBloggerCollect(BloggerCollection bloggerCollection){
        return bloggerCollectionRepository.save(bloggerCollection).getId();
    }

    public BloggerCollection getBloggerCollection(long bloggerId,long userId){
        val bloggerCollectionOptional = bloggerCollectionRepository.findByBloggerIdAndUserId(bloggerId, userId);
        return bloggerCollectionOptional.isPresent()?bloggerCollectionOptional.get():null;
    }

    public void cancelCollectBlogger(Long id){
        bloggerCollectionRepository.deleteById(id);
    }

    public Page<BloggerCollectResponse> getBloggerCollectPage(CollectSearchData searchData, Long userId){
        return bloggerCollectionRepository.getPage(searchData,userId);
    }

    public int getBloggerCollectCount(long userId){
        return bloggerCollectionRepository.countByUserId(userId);
    }
    //####################################################BloggerCollect END####################################################


    //####################################################MusicCollect START####################################################
    public Page<MusicResponse> getMusicCollectPage(int page, int pageSize, String keyword, Long userId){
        return musicCollectionRepository.getPage(page,pageSize,keyword,userId);
    }

    public long addMusicCollect(MusicCollection musicCollection){
        return musicCollectionRepository.save(musicCollection).getId();
    }

    public MusicCollection getMusicCollect(long musicId,long userId){
        val musicCollectionOptional = musicCollectionRepository.findByMidAndUserId(musicId, userId);
        return musicCollectionOptional.isPresent()?musicCollectionOptional.get():null;
    }

    public void cancelCollectMusic(Long id){
        musicCollectionRepository.deleteById(id);
    }

    public int getMusicCollectCount(long userId){
        return musicCollectionRepository.countByUserId(userId);
    }

    //####################################################MusicCollect END####################################################


    //####################################################ChallengeCollect START####################################################
    public Page<ChallengeResponse> getChallengeCollectPage(int page, int pageSize, String keyword, int sort, Long userId){
        return challengeCollectionRepository.getPage(page,pageSize,keyword,sort,userId);
    }

    public long addChallengeCollect(ChallengeCollection challengeCollection){
        return challengeCollectionRepository.save(challengeCollection).getId();
    }

    public ChallengeCollection getChallengeCollect(long cid,long userId){
        val challengeCollectionOptional = challengeCollectionRepository.findByCidAndUserId(cid, userId);
        return challengeCollectionOptional.isPresent()?challengeCollectionOptional.get():null;
    }

    public void cancelCollectChallenge(Long id){
        challengeCollectionRepository.deleteById(id);
    }

    public int getChallengeCount(long userId){
        return challengeCollectionRepository.countByUserId(userId);
    }

    //####################################################MusicCollect END####################################################


}
